package com.version1_4.dao.implement;

import com.version1_4.dao.FeedDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by reeveslee on 16/5/3.
 */
@Repository
public class FeedDaoImpl implements FeedDao {
    private static final Logger LOGGER = Logger.getLogger(FeedDaoImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 新添话题评论
     */
    @Override
    public boolean addFeed(int topicId, int userId, String text, String images, String voice) {
        String sql = "INSERT INTO feed(topicId,user,text,image,voice,time) VALUES (?,?,?,?,?,?)";
        int row = jdbcTemplate.update(sql, new Object[]{topicId, userId, text, images, voice, new Date()});
        return (row == 1) ? true : false;
    }

    @Override
    public List<Map<String, Object>> replys(int feedId) {
        String sql = "SELECT * FROM (SELECT temp1.id,temp1.userId,temp1.userName AS name,temp1.userPhoto AS photo,temp1.text,temp1.voice,temp1.time,temp2.userName AS subReplyTo FROM (SELECT id,feed_reply.userId,feedId,userName,userPhoto,text,voice,time,subReplyTo FROM feed_reply,user_info WHERE feed_reply.userId=user_info.userId) temp1,(SELECT id,userName FROM feed_reply,user_info WHERE feed_reply.userId=user_info.userId) temp2 WHERE temp1.subReplyTo=temp2.id AND temp1.feedId=? UNION SELECT id,feed_reply.userId,userName AS name,userPhoto AS photo,text,voice,time,'' FROM feed_reply,user_info WHERE feed_reply.userId=user_info.userId AND feed_reply.subReplyTo=0 AND feed_reply.feedId=?) t ORDER BY t.time ASC ";
        return jdbcTemplate.queryForList(sql, new Object[]{feedId, feedId});
    }

    @Override
    public boolean addReply(int userId, int feedId, int subReplyTo, String text, String voice) {
        String sql = "INSERT INTO feed_reply(userId,feedId,text,voice,time,subReplyTo) VALUES (?,?,?,?,?,?)";
        try {
            jdbcTemplate.update(sql, new Object[]{userId, feedId, text, voice, new Date(), subReplyTo});
            return true;
        } catch (Exception exception) {
            return false;
        }
    }

    @Override
    public boolean delReply(int userId, int replyId) {
        String sql = "DELETE FROM feed_reply WHERE userId=? AND id=?";
        try {
            jdbcTemplate.update(sql, new Object[]{userId, replyId});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<Map<String, Object>> newestFeed(int topicId) {
        String sql = "SELECT feed.id,userPhoto AS photo,userName AS name,text,image,voice,time FROM feed,user_info WHERE feed.user=user_info.userId AND feed.topicId=? ORDER BY time DESC LIMIT 2";
        return jdbcTemplate.queryForList(sql, new Object[]{topicId});
    }

    @Override
    public int newFeedNum(int topicId, Date time) {
        String sql = "SELECT COUNT(id) FROM feed WHERE feed.topicId=? AND time>?";
        return jdbcTemplate.queryForObject(sql, new Object[]{topicId, time}, java.lang.Integer.class);
    }

    @Override
    public boolean addPraise(int feedId, int userId) {
        try {
            String sql = "SELECT id FROM feed_praise WHERE userId=? AND feedId=?";
            jdbcTemplate.queryForObject(sql, new Object[]{userId, feedId}, java.lang.Integer.class);
            return false;
        } catch (EmptyResultDataAccessException exception) {
            String sql = "INSERT INTO feed_praise(userId,feedId,time) VALUES (?,?,?)";
            int row = jdbcTemplate.update(sql, new Object[]{userId, feedId, new Date()});
            return (row == 1) ? true : false;
        }
    }

    @Override
    public boolean deletePraise(int feedId, int userId) {
        try {
            String sql = "DELETE FROM feed_praise WHERE userId=? AND feedId=?";
            int row = jdbcTemplate.update(sql, new Object[]{userId, feedId});
            return (row == 1) ? true : false;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Map<String, Object> feedInfo(int feedId) {
        try {
            String sql = "SELECT feed.id,userId,text,image,voice,time,reply,praise,userName AS name,userPhoto AS photo FROM feed,user_info WHERE id=? AND feed.user=user_info.userId";
            return jdbcTemplate.queryForMap(sql, new Object[]{feedId});
        } catch (Exception exception) {
            return null;
        }
    }

    @Override
    public boolean collectAdd(int userId, int feedId) {
        try {
            String sql = "SELECT id FROM feed_collect WHERE userId=? AND feedId=?";
            jdbcTemplate.queryForObject(sql, new Object[]{userId, feedId}, java.lang.Integer.class);
            return true;
        } catch (EmptyResultDataAccessException exception) {
            String sql = "INSERT INTO feed_collect(userId,feedId,time) VALUES (?,?,?)";
            int row = jdbcTemplate.update(sql, new Object[]{userId, feedId, new Date()});
            return (row == 1) ? true : false;
        }

    }

    @Override
    public List<Map<String, Object>> collect(int userId) {
        try {
            String sql = "SELECT feed.id,feed.text,feed.image,feed.voice,feed.time,userName AS name,userPhoto AS photo FROM feed_collect,feed,user_info WHERE feed_collect.feedId=feed.id AND feed.user=user_info.userId AND feed_collect.userId=? ORDER BY feed_collect.id DESC";
            return jdbcTemplate.queryForList(sql, new Object[]{userId});
        } catch (DataAccessException exception) {
            return null;
        }
    }

    @Override
    public boolean report(int userId, int feedId) {
        try {
            String sql = "SELECT id FROM feed_suspicious WHERE userId=? AND feedId=?";
            jdbcTemplate.queryForObject(sql, new Object[]{userId, feedId}, java.lang.Integer.class);
            return true;
        } catch (Exception exception) {
            String sql = "INSERT INTO feed_suspicious(userId,feedId,time) VALUES (?,?,?)";
            int row = jdbcTemplate.update(sql, new Object[]{userId, feedId, new Date()});
            return (row == 1) ? true : false;
        }
    }

    @Override
    public boolean collectDel(int userId, int feedId) {
        String sql = "DELETE FROM feed_collect WHERE userId=? AND feedId=?";
        int row = jdbcTemplate.update(sql, new Object[]{userId, feedId});
        return (row == 1) ? true : false;
    }

    @Override
    public boolean delete(int userId, int feedId) {
        String sql = "DELETE FROM feed WHERE user=? AND id=?";
        try {
            jdbcTemplate.update(sql, new Object[]{userId, feedId});
            sql = "DELETE FROM feed_collect WHERE feedId=?";
            jdbcTemplate.update(sql, new Object[]{feedId});
            sql = "DELETE FROM feed_reply WHERE feedId=?";
            jdbcTemplate.update(sql, new Object[]{feedId});
            sql = "DELETE FROM feed_praise WHERE userId=? AND feedId=?";
            jdbcTemplate.update(sql, new Object[]{userId, feedId});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Map<String, Object> replyPushInfo(int userId, int feedId, int subReplyTo) {
        try {
            if (subReplyTo == 0) {
                String sql = "SELECT a.userName AS name,b.baiduId FROM user_info AS a,user_info AS b,feed where a.userId=? AND feed.user=b.userId AND feed.id=?";
                return jdbcTemplate.queryForMap(sql, new Object[]{userId, feedId});
            } else {
                String sql = "SELECT a.userName AS name,b.baiduId FROM user_info AS a,user_info AS b,feed_reply where a.userId=? AND feed_reply.userId=b.userId AND feed_reply.id=?";
                return jdbcTemplate.queryForMap(sql, new Object[]{userId, subReplyTo});
            }
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Map<String, Object> praisePushInfo(int userId, int feedId) {
        try {
            String sql = "SELECT a.userName AS name,b.baiduId FROM user_info AS a,user_info AS b,feed where a.userId=? AND feed.user=b.userId AND feed.id=?";
            return jdbcTemplate.queryForMap(sql, new Object[]{userId, feedId});
        } catch (Exception e) {
            return null;
        }
    }
}
